
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

import DBM.DBManager;
import bean.EquipmentBEAN;

public class EquipmentDAO {
	private final String	addEquipment		= "INSERT INTO Equipment VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
	private final String	getEquipmentById	= "SELECT * FROM Equipment WHERE equipmentID = ?";
	private final String	updateEquipment		= "UPDATE Equipment SET sniper = ?, bomb = ?, uziSMG = ?, desertEagle = ?,"
														+ "barakSP21 = ?, coltM1911 = ?, knife = ?, bulletFreeArmor = ?, bulletFreeHelmet = ?,"
														+ "bombArmor = ?, bodyGuard = ?, bullet = ? WHERE equipmentID = ?";
	private DBManager		dbmanager			= null;

	public EquipmentDAO( DBManager DBM ) {
		this.dbmanager = DBM;
	}

	public void addEquipment( int characterID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( addEquipment );
			ppstm.setInt( 1, characterID );
			ppstm.setBoolean( 2, false );
			ppstm.setBoolean( 3, false );
			ppstm.setBoolean( 4, false );
			ppstm.setBoolean( 5, false );
			ppstm.setBoolean( 6, false );
			ppstm.setBoolean( 7, false );
			ppstm.setBoolean( 8, false );
			ppstm.setBoolean( 9, false );
			ppstm.setBoolean( 10, false );
			ppstm.setBoolean( 11, false );
			ppstm.setInt( 12, 0 );
			ppstm.setInt( 13, 0 );
			ppstm.execute();
		} catch( SQLIntegrityConstraintViolationException e ) {
			e.printStackTrace();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public EquipmentBEAN getEquipmentByID( int characterID ) {
		EquipmentBEAN equipmentBEAN = null;
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( getEquipmentById );
			ppstm.setInt( 1, characterID );
			ResultSet res = ppstm.executeQuery();
			while( res.next() ) {
				equipmentBEAN = new EquipmentBEAN( res.getInt( 1 ), res
						.getBoolean( 2 ), res.getBoolean( 3 ), res
						.getBoolean( 4 ), res.getBoolean( 5 ), res
						.getBoolean( 6 ), res.getBoolean( 7 ), res
						.getBoolean( 8 ), res.getBoolean( 9 ), res
						.getBoolean( 10 ), res.getBoolean( 11 ), res
						.getInt( "bodyguard" ), res.getInt( "bullet" ) );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return equipmentBEAN;
	}

	public void updateEquipment( EquipmentBEAN equipmentBEAN ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( updateEquipment );
			ppstm.setBoolean( 1, equipmentBEAN.isSniperRifle() );
			ppstm.setBoolean( 2, equipmentBEAN.isBomb() );
			ppstm.setBoolean( 3, equipmentBEAN.isUziSMG() );
			ppstm.setBoolean( 4, equipmentBEAN.isDesertEagle() );
			ppstm.setBoolean( 5, equipmentBEAN.isBarakSP21() );
			ppstm.setBoolean( 6, equipmentBEAN.isColtM1911() );
			ppstm.setBoolean( 7, equipmentBEAN.isKnife() );
			ppstm.setBoolean( 8, equipmentBEAN.isBulletFreeArmor() );
			ppstm.setBoolean( 9, equipmentBEAN.isBulletFreeHelmet() );
			ppstm.setBoolean( 10, equipmentBEAN.isBombArmor() );
			ppstm.setInt( 11, equipmentBEAN.getBodyguard() );
			ppstm.setInt( 12, equipmentBEAN.getBullet() );
			ppstm.setInt( 13, equipmentBEAN.getEquipmentID() );
			ppstm.execute();
		} catch( SQLIntegrityConstraintViolationException e ) {
			e.printStackTrace();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}
}
